<?php
	//ob_start();
	//require_once('start_session.php');
	require_once('db_connect.php');

	$user_id=$_POST['user_id'];
	$passwd=$_POST['passwd'];
  if(!$user_id){
    echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
        <script>
            window.alert('아이디를 입력하세요')
            history.go(-1)
        </script>
        ");
        exit;
  }
  
  if(!$passwd){
    echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
        <script>
            window.alert('비밀번호를 입력하세요')
            history.go(-1)
        </script>
        ");
        exit;
  } 
  
  $sql="select *from member where id='$user_id'";
  $result=mysqli_query($conn,$sql);
  
  $num_match=mysqli_num_rows($result);
  
  if(!$num_match)
  {
      echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
        <script>
            window.alert('등록되지 않은 아이디입니다')
            history.go(-1)
        </script>
          ");
  }
  else
  {
      $row=mysqli_fetch_array($result);
      
      $db_passwd=$row[passwd];
      
      if($passwd != $db_passwd)
      {
        echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
          <script>
              window.alert('비밀번호가 틀립니다')
              history.go(-1)
          </script>
        ");
      
      exit;
      }
      else
      { 		
		setcookie('user_id',$user_id);
		require_once('start_session.php');
		//관리자가 로그인할 경우 접속정보 없앰
		if($user_id=='admin')
		{	
			$sql="DELETE FROM session WHERE ip='$_SESSION[ip]'";
			mysqli_query($conn,$sql);
			//$_SESSION['ip']='';
			//$_SESSION['location']='';
			//$_SESSION['connect_time']='';
			Header("location:user_info.php");
		}			
		
		$sql="update session set id = '$user_id' where ip='$_SESSION[ip]'";
		mysqli_query($conn,$sql);
		echo("
          <script>
          top.location.href='main.php';
          </script>
          ");
      }
  }
  ?>